package cn.ren.controller;

import cn.ren.constant.MessageConstant;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.CheckGroup;
import cn.ren.pojo.CheckItem;
import cn.ren.service.CheckGorupService;
import cn.ren.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: itcast_health
 * @description: 检查组控制器
 * @author: ren
 * @create: 2020-02-22 16:50
 **/
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGorupService checkGorupService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> list = checkGorupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGorupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGorupService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findAllGroup")
    public Result findAllGroup(){
        List<CheckGroup> list = checkGorupService.findAllGroup();
        if (list != null && list.size() > 0){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            CheckGroup checkGroup = checkGorupService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(int id){
        try {
            List<Integer> checkItemIds = checkGorupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGorupService.edit(checkGroup,checkitemIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
}
